<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Encrypt extends CI_Encrypt {
	
	var $_hash_type	= 'md5';
	
	function __construct(){
		parent::__construct();
	}
	/**
     * Generate a [salted] hash.
     *
     * $salt can be:
     * false - a random will be generated
     * integer - a random with specified length will be generated
     * string
     *
     * @param string $password
     * @param mixed $salt
     * @return string
     */
	function get_hash($password,$salt = false){
		if (is_integer($salt)){
			$this->CI->load->helper('string');
			$salt = random_string('alnum',$salt);
		}
		return $salt === false ? $this->hash($password) : $this->hash($salt . $password) . ':' . $salt;
	}
	
	 /**
     * Validate hash against hashing method (with or without salt)
     *
     * @param string $password
     * @param string $hash
     * @return bool
     * @throws Exception
     */
	function validate_hash($password,$hash){
	 	$hashArr = explode(':', $hash);
        switch (count($hashArr)) {
            case 1:
                return $this->hash($password) === $hash;
            case 2:
                return $this->hash($hashArr[1] . $password) === $hashArr[0];
        }
        show_error('Invalid hash.');
	}
}
